package com.skcc.ts.nsi.expense;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.skcc.ts.nsi.R;
import com.skcc.ts.nsi.service.MEDataHandler;
import com.skcc.ts.nsi.service.SocketClientService;

//허가득 SMS미정산 내역 리스트를 보여주는 화면.
public class MEExpenseListMjsSms extends Activity {

	private String myId;
	private String exeYN = "";
	private String SMS;
	private String[] expBillCode;
	private String[] expBillDate;
	private String[] expBillAmount;
	private String[] expShopName;
	private String[] expShopLocation;
	private String[] expState;
	private String[] expProcessDate;
	private String[] expType;
	private String[] pjtCode;
	private String[] pjtName;
	private String[] acntCode;
	private String[] acntName;
	String expShopLocation_temp;
	private String addr;
	private String port;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.meexpense_list_mjs_sms);

		getSmsMjsList();// 미정산목록정보 수신

		if (exeYN.equals("Y")) {
			TableLayout numberTable = (TableLayout) findViewById(R.id.expenseTable_mjs_sms);
			TableLayout numberTable2 = (TableLayout) findViewById(R.id.expenseTable_mjs_sms);
			TableLayout numberTable3 = (TableLayout) findViewById(R.id.expenseTable_mjs_sms);

			TableRow tableR = null;
			TableRow tableR2 = null;
			TableRow tableR3 = null;

			for (int i = 0; i < expBillCode.length; i++) {
				tableR = new TableRow(this);
				tableR.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

				tableR2 = new TableRow(this);
				tableR2.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

				tableR3 = new TableRow(this);
				tableR3.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

				final TextView text_bill_date_and_bill_amount_list = new TextView(this);
				final TextView text_shop_name_list = new TextView(this);
				final TextView text_shop_location_list = new TextView(this);

				final TextView text_bill_code = new TextView(this);
				final TextView text_bill_date = new TextView(this);
				final TextView text_bill_amount = new TextView(this);
				final TextView text_shop_name = new TextView(this);
				final TextView text_shop_location = new TextView(this);
				final TextView text_state = new TextView(this);
				final TextView text_process_date = new TextView(this);
				final TextView text_type = new TextView(this);

				if (i % 2 == 1) {
					tableR.setBackgroundColor(0xffd3daeb);
					tableR2.setBackgroundColor(0xffd3daeb);
					tableR3.setBackgroundColor(0xffd3daeb);
				} else {
					tableR.setBackgroundColor(Color.WHITE);
					tableR2.setBackgroundColor(Color.WHITE);
					tableR3.setBackgroundColor(Color.WHITE);
				}
				// 정산내역목록 리스트에 뿌려주기 위해 각종 string 을 붙여 사용하게끔 추가.
				text_bill_date_and_bill_amount_list.setText("\t" + expBillDate[i].substring(0, 10).replace("-", "/") + "\t\t사용금액: "
						+ MEDataHandler.addComma(expBillAmount[i]) + "원");
				text_bill_date_and_bill_amount_list.setTextColor(Color.BLACK);
				text_bill_date_and_bill_amount_list.setTextSize(18);

				text_shop_name_list.setText("\t" + "사용처 : " + expShopName[i]);
				text_shop_name_list.setTextColor(Color.BLACK);
				text_shop_name_list.setTextSize(15);
				if (expShopLocation[i].equals(" ")) {
					expShopLocation_temp = "내용없음";
				} else {
					expShopLocation_temp = expShopLocation[i];
				}
				text_shop_location_list.setText("\t" + "사용장소 : " + expShopLocation_temp);
				text_shop_location_list.setTextColor(Color.BLACK);
				text_shop_location_list.setTextSize(15);

				// 데이터 주고받기위해 다른문자들이 추가되면 안됨.
				text_bill_code.setText(expBillCode[i]);
				text_bill_date.setText(expBillDate[i]);
				text_bill_amount.setText(expBillAmount[i]);
				text_shop_name.setText(expShopName[i]);
				text_shop_location.setText(expShopLocation[i]);
				text_state.setText(expState[i]);
				text_process_date.setText(expProcessDate[i]);
				text_type.setText(expType[i]);

				tableR.addView(text_bill_date_and_bill_amount_list);
				tableR2.addView(text_shop_name_list);
				tableR3.addView(text_shop_location_list);

				numberTable.addView(tableR, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
				numberTable2.addView(tableR2, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
				numberTable3.addView(tableR3, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

				tableR2.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						Intent I = new Intent(MEExpenseListMjsSms.this, MEExpenseRegister.class);
						I.putExtra("myId", myId);// 사번
						I.putExtra("expRegi", true);
						I.putExtra("billCode", text_bill_code.getText());
						I.putExtra("billDate", text_bill_date.getText());
						I.putExtra("billAmount", text_bill_amount.getText());
						I.putExtra("shopName", text_shop_name.getText());
						I.putExtra("shopLocation", text_shop_location.getText());
						I.putExtra("state", text_state.getText());
						I.putExtra("processDate", text_process_date.getText());
						I.putExtra("type", text_type.getText());
						I.putExtra("pjtCode", pjtCode);
						I.putExtra("pjtName", pjtName);
						I.putExtra("acntCode", acntCode);
						I.putExtra("acntName", acntName);
						I.putExtra("loginAddr", addr);
						I.putExtra("loginPort", port);
						I.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
						startActivity(I);
						// finish();
					}
				});
			}
		} else {

		}
	}

	private void getSmsMjsList() {
		Intent receiveI = getIntent();
		boolean doYN = receiveI.getBooleanExtra("start", false);
		// String param = receiveI.getStringExtra("param");
		if (doYN) {

			myId = receiveI.getStringExtra("myId");
			SMS = receiveI.getStringExtra("SMS");
			expBillCode = receiveI.getStringArrayExtra("billCode");
			expBillDate = receiveI.getStringArrayExtra("billDate");
			expBillAmount = receiveI.getStringArrayExtra("billAmount");
			expShopName = receiveI.getStringArrayExtra("shopName");
			expShopLocation = receiveI.getStringArrayExtra("shopLocation");
			expState = receiveI.getStringArrayExtra("state");
			expProcessDate = receiveI.getStringArrayExtra("processDate");
			expType = receiveI.getStringArrayExtra("type");
			pjtCode = receiveI.getStringArrayExtra("pjtCode");
			pjtName = receiveI.getStringArrayExtra("pjtName");
			acntCode = receiveI.getStringArrayExtra("acntCode");
			acntName = receiveI.getStringArrayExtra("acntName");
			addr = receiveI.getStringExtra("loginAddr");
			port = receiveI.getStringExtra("loginPort");

			Log.d("SMS미정산리스트", "들어옴");
			if ("No Data Found".equals(expBillCode[0])) {
				exeYN = "N";
				new AlertDialog.Builder(MEExpenseListMjsSms.this).setTitle("알림").setMessage("SMS로 수신된 미정산내역이 없습니다.")
						.setPositiveButton("OK", new DialogInterface.OnClickListener() {

							public void onClick(DialogInterface dialog, int which) {
								Intent expListMjsSms = new Intent(MEExpenseListMjsSms.this, SocketClientService.class);
								expListMjsSms.putExtra("id", 43);// 기능분류(미정산내역)
								expListMjsSms.putExtra("myId", myId);
								expListMjsSms.putExtra("loginAddr", addr);
								expListMjsSms.putExtra("loginPort", port);
								expListMjsSms.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
								startService(expListMjsSms);
								finish();

							}
						}).show();

			} else {
				if (SMS.equals("true")) {
					new AlertDialog.Builder(MEExpenseListMjsSms.this).setTitle("알림").setMessage("SMS로 수신된 미정산내역을 확인해주세요.")
							.setPositiveButton("OK", null).show();
				}
				exeYN = "Y";
			}

		}
	}

}
